package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceimpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1:page;
        int offSet = (page - 1)*page;
        List<Goods> list = goodsDao.getGoodsInventoryList(offSet, rows, codeOrName, goodsTypeId);
        map.put("rows",list);
        return map;
    }

    @Override
    public Map list(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        Map<String,Object>map = new HashMap<>();
        page = page==0?1:page;
        int offSet = (page-1)*rows;
        List<Goods>list = goodsDao.list(offSet,rows,goodsName,goodsTypeId);
        map.put("rows",list);
        return map;
    }

    @Override
    public ServiceVO save(Goods goods) {
        Integer goodsId = goods.getGoodsId();
        if (goods==null){
            goods.setLastPurchasingPrice(goods.getPurchasingPrice());
            goods.setInventoryQuantity(0);
            goods.setState(0);
            goodsDao.save(goods);
        }else {
            goodsDao.update(goods);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
}
